package com.wisdudu.blank.ui.user;

import android.databinding.ObservableField;

import com.kelin.mvvmlight.base.ViewModel;
import com.kelin.mvvmlight.command.ReplyCommand;
import com.wisdudu.blank.data.Injection;
import com.wisdudu.blank.data.repo.UserRepo;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.ProgressSubscriber;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.SubscriberOnNextListener;
import com.wisdudu.blank.support.rxbus.RxBus;
import com.wisdudu.blank.support.util.ToastUtil;
import com.wisdudu.blank.support.widget.loading.LoadingProgressDialog;

/**
 * Created by ypp on 2016/8/29.
 */
public class UserSexCheckVM implements ViewModel {
    public ObservableField<Integer> sex = new ObservableField<>();
    private UserRepo userRepo;
    private UserSexCheckFragment mFragment;

    public UserSexCheckVM(UserSexCheckFragment fragment, int sex) {
        this.mFragment = fragment;
        userRepo = Injection.provideUserRepo();
        this.sex.set(sex);
    }

    public ReplyCommand onManClick = new ReplyCommand(() -> {
        sex.set(1);
    });

    public ReplyCommand onWomanClick = new ReplyCommand(() -> {
        sex.set(2);
    });

    public void confirm() {
        userRepo.editSex(sex.get()).compose(mFragment.bindToLifecycle())
                .subscribe(new ProgressSubscriber<>(new SubscriberOnNextListener<Object>() {
                    @Override
                    public void onNext(Object o, LoadingProgressDialog pd) {
                        ToastUtil.INSTANCE.toast("修改成功");
                        RxBus.getDefault().post(sex.get());
                        mFragment.removeFragment();
                    }
                }, mFragment.mActivity, "正在提交...", 1000));
    }

}
